package com.rightsidetech.user.impl;

import com.rightsidetech.commom.base.ResponseData;
import com.rightsidetech.commom.enums.CodeIdEnum;
import com.rightsidetech.user.dtos.requst.IdReq;
import com.rightsidetech.user.dtos.requst.UserAddAddressReq;
import com.rightsidetech.user.dtos.requst.UserUpdateAddressReq;
import com.rightsidetech.user.dtos.response.UserAddressResp;
import com.rightsidetech.user.entity.UsUserAddress;
import com.rightsidetech.user.mapper.UsUserAddressMapper;
import com.rightsidetech.user.service.UserAddressService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @Package: com.rightsidetech.user.impl
 * @ClassName: UserAddressServiceImpl
 * @Author: yinj
 * @CreateTime: 2021/10/25 14:08
 * @Description: TODO
 */
@Service("userAddressServiceImpl")
public class UserAddressServiceImpl implements UserAddressService {

    @Resource
    private UsUserAddressMapper usUserAddressMapper;

    @Override
    public ResponseData save(UserAddAddressReq req, Long userInfoId) {

        UsUserAddress address = new UsUserAddress();
        address.setUserInfoId(userInfoId);
        address.setUserName(req.getUserName());
        address.setUserPhone(req.getUserPhone());
        address.setProvince(req.getProvice());
        address.setCity(req.getCity());
        address.setArea(req.getArea());
        address.setAddress(req.getAddress());
        address.setDoorplate(req.getAddressDetails());
        address.setLongitude(req.getLongitude());
        address.setLatitude(req.getLatitude());

        List<UserAddressResp> addressList = usUserAddressMapper.findByUserInfoId(userInfoId);
        if (CollectionUtils.isEmpty(addressList)) {
            address.setIsDefault((byte) 1);
        } else {
            address.setIsDefault(req.getIsDefault());
        }
        address.setCreatedAt(new Date());

        //判断是否有重复的地址
        UsUserAddress usUserAddress2 = usUserAddressMapper.selectByusUserAddress(address);
        if (!StringUtils.isEmpty(usUserAddress2)) {
            return ResponseData.fail("地址重复");
        }

        int count = usUserAddressMapper.insertSelective(address);
        if (count <= 0) {
            return ResponseData.fail("添加失败");
        }

        //判断默认地址
        if (req.getIsDefault() == 1) {
            //修改其它地址为非默认地址
            usUserAddressMapper.updateIsDefaultByUserInfoId(userInfoId, (byte) 0, address.getId());
        }
        return ResponseData.success();
    }

    @Override
    public ResponseData update(UserUpdateAddressReq req, Long userInfoId) {

        UsUserAddress usUserAddress = usUserAddressMapper.selectByPrimaryKey(req.getId());
        if (ObjectUtils.isEmpty(usUserAddress)){
            return ResponseData.fail(CodeIdEnum.EMPTY_DATA);
        }

        UsUserAddress address = new UsUserAddress();
        address.setId(req.getId());
        address.setUserInfoId(userInfoId);
        address.setUserName(req.getUserName());
        address.setUserPhone(req.getUserPhone());
        address.setProvince(req.getProvice());
        address.setCity(req.getCity());
        address.setArea(req.getArea());
        address.setAddress(req.getAddress());
        address.setDoorplate(req.getAddressDetails());
        address.setLongitude(req.getLongitude());
        address.setLatitude(req.getLatitude());
        address.setIsDefault(req.getIsDefault());
        address.setCreatedAt(new Date());

        //判断是否有重复的地址
        UsUserAddress usUserAddress2 = usUserAddressMapper.selectByusUserAddress(address);
        if (!StringUtils.isEmpty(usUserAddress2) && !usUserAddress2.getId().equals(usUserAddress.getId())) {
            return ResponseData.fail("地址重复");
        }

        int count = usUserAddressMapper.updateByPrimaryKeySelective(address);
        if (count <= 0) {
            return ResponseData.fail("修改失败");
        }

        //判断默认地址
        if (req.getIsDefault() == 1) {
            //修改其它地址为非默认地址
            usUserAddressMapper.updateIsDefaultByUserInfoId(userInfoId, (byte) 0, address.getId());
        }
        return ResponseData.success();
    }

    @Override
    public ResponseData<List<UserAddressResp>> list(Long userInfoId) {
        List<UserAddressResp> addressResps = usUserAddressMapper.findByUserInfoId(userInfoId);
        return ResponseData.success(addressResps);
    }

    @Override
    public ResponseData del(IdReq req, Long userInfoId) {
        UsUserAddress usUserAddress = usUserAddressMapper.selectByPrimaryKey(req.getId());
        if(StringUtils.isEmpty(usUserAddress)){
            return ResponseData.fail("地址不存在");
        }
        if(!usUserAddress.getUserInfoId().equals(userInfoId)){
            return ResponseData.fail("地址不存在");
        }

        usUserAddressMapper.del(req.getId(), userInfoId);
        return ResponseData.success();
    }

    @Override
    public ResponseData updateIsDefault(Long userInfoId, IdReq req) {
        UsUserAddress usUserAddress = usUserAddressMapper.selectByPrimaryKey(req.getId());
        if(StringUtils.isEmpty(usUserAddress)){
            return ResponseData.fail("地址不存在");
        }
        if(!usUserAddress.getUserInfoId().equals(userInfoId)){
            return ResponseData.fail("地址不存在");
        }
        if(usUserAddress.getIsDefault() == 1){
            return ResponseData.fail("已经是默认地址了");
        }
        usUserAddressMapper.updateIsDefault(req.getId());
        usUserAddressMapper.updateIsDefaultByUserInfoId(userInfoId,(byte)0,req.getId());

        return ResponseData.success();
    }
}
